package com.jbh.wanandroidkotlin.fragment

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.jbh.wanandroidkotlin.CommonDataBean
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.activity.*
import com.jbh.wanandroidkotlin.manager.UserInfoManager
import kotlinx.android.synthetic.main.fragment_index.*
import kotlinx.android.synthetic.main.fragment_index.mRecyclerView
import kotlinx.android.synthetic.main.fragment_user_center.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class UserCenterFragment : Fragment() {

    private var param1: String? = null
    private var param2: String? = null
    private var meAdapter : MeAdapter ?= null
    private var list = mutableListOf<CommonDataBean>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_user_center, container, false)
    }


    companion object {

        @JvmStatic
        fun newInstance(param1: String = "", param2: String= "") =  UserCenterFragment()
            .apply {
                arguments = Bundle()
                    .apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mRecyclerView.apply {
            val l = LinearLayoutManager(activity)
            l.orientation = LinearLayoutManager.VERTICAL
            layoutManager = l

            val d=DividerItemDecoration(context,RecyclerView.VERTICAL)
            val drawable = GradientDrawable()
            drawable.setSize(0,1)
            drawable.setColor(Color.BLACK)
            drawable.alpha = 50
            d.setDrawable(drawable)
            addItemDecoration(d)
            meAdapter =  MeAdapter(getList())
            adapter = meAdapter

            meAdapter!!.setOnItemClickListener { adapter, view, position ->
                val commonDataBean = list[position]
                Log.v("[===]","commonDataBean = ${Gson().toJson(commonDataBean)}")
                click(commonDataBean)
            }
        }

        var userInfo = UserInfoManager.newInstance().userInfo()

        Log.v("[===]","userInfo = ${Gson().toJson(userInfo)}")
        userInfo?.run {
            mUserEnterHeadView.setNickName(username)
        }
    }


    /**
     *
     */
    private fun click(commonDataBean: CommonDataBean) {
        when(commonDataBean.type){
            0 ->{
                toActivity(IntegralListActivity::class.java)
            }
            1 ->{
                toActivity(ShareListActivity::class.java)
            }
            2 ->{
                toActivity(FavoriteListActivity::class.java)
            }
            3 ->{
                toActivity(LaterYDListActivity::class.java)
            }
            4 ->{
                toActivity(YDHirListActivity::class.java)
            }
            5 ->{
                toActivity(SettingListActivity::class.java)
            }
        }
    }




    private fun <T> toActivity(cls:Class<T>){
        activity?.startActivity(Intent(activity,cls))
    }




    private fun getList(): ArrayList<CommonDataBean> {
        list.clear()
        list.add(CommonDataBean("我的积分",type = 0))
        list.add(CommonDataBean("我的分享",type = 1))
        list.add(CommonDataBean("我的收藏",type = 2))
        list.add(CommonDataBean("稍后阅读",type = 3))
        list.add(CommonDataBean("阅读历史",type = 4))
        list.add(CommonDataBean("系统设置",type = 5))
        return list as ArrayList<CommonDataBean>
    }



    class MeAdapter(val list:ArrayList<CommonDataBean>) :
        BaseQuickAdapter<CommonDataBean,BaseViewHolder>(R.layout.item_me_list_layout,list){
        override fun convert(helper: BaseViewHolder, item: CommonDataBean) {
            helper.setText(R.id.tv_title,item.title)
        }
    }



}